/*
 * Copyright 2007. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the GNU Lesser General Public License, Version 2. You
 * can find a copy of the license at:
 * 
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */
package com.sinai.mshab.client.ui.basic;

import java.util.Iterator;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.sinai.mshab.client.util.Answer;
import com.sinai.mshab.client.ui.FormComponent;
import com.sinai.mshab.client.ui.aggregate.DataReceiver;
import com.sinai.mshab.client.ui.aggregate.FormData;

/**
 * This component contains 1 checkbox. Essentially a CheckBox class which
 * implements our getAnswer() and populate()
 * 
 * @author serge
 * 
 */
public class CheckBoxComponent extends BasicFormComponent {

	/** need to hang on to the widget */
	private CheckBox checkbox;
	private String name;

	/**
	 * Creates a new GWT checkbox with a db-fieldname
	 * 
	 * @param fName
	 *            the name of the field in the database where this widgets
	 *            answer will be stored
	 * @param name
	 *            the name of the checkbox
	 */
	public CheckBoxComponent(String dbfieldName, String tableName, String description) {
		checkbox  = new CheckBox(description);
		super.initialize(dbfieldName, tableName, null, null,
				null, checkbox, true);
	}

	/**
	 * 
	 * 
	 * @param dbfieldName
	 *            the name of the field to which the textbox information
	 *            corresponds to.
	 * @param tableName
	 *            the name of the database table where the textbox information
	 *            will be stored.
	 * @param description
	 *            The description label that goes before textbox (eg. Name)
	 * @param quantifier
	 *            The quantifier label that goes after textbox (eg. /years)
	 */
	public CheckBoxComponent(String dbfieldName, String tableName,
			String description, String quantifier) {
		checkbox  = new CheckBox(description);
		super.initialize(dbfieldName, tableName, null, quantifier,
				null, checkbox, true);
	}
	
	/**
	 * 
	 * 
	 * @param dbfieldName
	 *            the name of the field to which the textbox information
	 *            corresponds to.
	 * @param tableName
	 *            the name of the database table where the textbox information
	 *            will be stored.
	 * @param description
	 *            The description label that goes before textbox (eg. Name)
	 * @param quantifier
	 *            The quantifier label that goes after textbox (eg. /years)
	 * @param error_msg
	 *            The error message that gets displayed if the attached
	 *            validators do not pass
	 */
	public CheckBoxComponent(String dbfieldName, String tableName,
			String description, String quantifier, String error_msg) {
		checkbox  = new CheckBox(description);
		super.initialize(dbfieldName, tableName, null, quantifier,
				error_msg, checkbox, true);
	}

	public void sendAnswer(DataReceiver receiver) {
		String checked = checkbox.isChecked() ? "Yes" : "No";

		Answer answer = new Answer();
		answer.add(tableName, dbfieldName, checked);
		answer.add(hiddenFields);
		receiver.sendData(answer);
	}

	public Answer getAnswer() {
		String checked = checkbox.isChecked() ? "Yes" : "No";

		Answer answer = new Answer();
		answer.add(tableName, dbfieldName, checked);
		answer.add(hiddenFields);
		return answer;
	}

	/**
	 * @see FormComponent#setEnabled(boolean)
	 */
	public void setEnabled(boolean enabled) {
		checkbox.setEnabled(enabled);
	}

	/**
	 * @see FormComponent#addClickListener(ClickListener)
	 */
	public void addClickListener(ClickListener listener) {
		checkbox.addClickListener(listener);
	}

	/**
	 * If <code>checked</code> is true, check this
	 * <code>CheckBoxComponent</code>, otherwise uncheck it.
	 * 
	 * @param checked
	 *            if true, check this <code>CheckBoxComponent</code>,
	 *            otherwise uncheck it.
	 */
	public void setChecked(boolean checked) {
		checkbox.setChecked(checked);
	}

	public Object clone() {
		return new CheckBoxComponent(dbfieldName, tableName, name);
	}

	/**
	 * Populate this widget with FormData using an Answer object
	 * 
	 * @param answer
	 *            the answer object containing the data.
	 */
	public boolean populate(Answer answer) {

		if (answer == null) {
			return false;
		}

		// walking through all the fields-values of answer
		Iterator field_names = answer.getHashMap().keySet().iterator();
		while (field_names.hasNext()) {
			String field_name = (String) field_names.next();

			// if the db field name matches,
			if (field_name.equalsIgnoreCase(dbfieldName)) {
				String value = (String) answer.getHashMap().get(field_name);
				if (value != null && value.equalsIgnoreCase("Yes")) {
					checkbox.setChecked(true);
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * Populate this widget with FormData
	 */
	public boolean populate() {
		Answer a = FormData.getData(tableName);
		return populate(a);
	}

	public HorizontalPanel toReport() {

		String answer = "";

		// if the checkbox is checked, then we return what our text is.

		if (checkbox.isChecked()) {
			answer = checkbox.getText();
		}

		return super.toPrint(answer);
	}

	/**
	 * @see FormComponent#isEmpty()
	 */
	public boolean isEmpty() {

		// if the checkbox isn't checked, then it's empty.
		return !checkbox.isChecked();
	}
}
